Skip to content

fix(core): guard waterfall next against double invocation - #83

Open
rekcilyssup wants to merge 1 commit into
cordiverse:mainfrom
rekcilyssup:fix/waterfall-double-next
Open

fix(core): guard waterfall next against double invocation#83
rekcilyssup wants to merge 1 commit into
cordiverse:mainfrom
rekcilyssup:fix/waterfall-double-next

Conversation

@rekcilyssup

Copy link
Copy Markdown

Closes #42

Problem

ctx.waterfall() passed a single shared next continuation to every
middleware frame. Calling it twice in one frame advanced the shared queue
again and re-invoked the terminal callback, duplicating terminal side
effects.

Fix

In packages/core/src/events.ts, each middleware frame now receives its own
guarded continuation:

  • the guard is scoped to a single frame — calling the same next twice
    throws next() should only be called once;
  • the chain still passes the current continuation as the last argument to
    every listener and the terminal, preserving the existing contract used by
    internal/update, internal/get, and internal/set;
  • valid sync, async, and nested chains are unaffected: frames advance exactly
    once and the terminal runs exactly once.

Tests

Added 3 tests: double next() in one frame throws while the terminal runs
once, reusing a previous frame's next throws, and an async listener calling
next() after await still completes the chain in order.

Verified locally: yarn lint, yarn build, yarn test (170 passed).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: waterfall invokes the terminal twice when middleware reuses next()

1 participant